package cn.btks.resultintegration.achievements.service.impl;

import cn.btks.commonserver.result.Constants;
import cn.btks.commonserver.result.ResultState;
import cn.btks.commonserver.result.StateInfo;
import cn.btks.commonserver.util.FileUtil;
import cn.btks.resultintegration.achievements.pojo.TextAchievements;
import cn.btks.resultintegration.achievements.pojo.TextAchievementsTemp;
import cn.btks.resultintegration.achievements.repository.TextAchievementsTempRepository;
import cn.btks.resultintegration.achievements.service.TextAchievementsTempService;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @Author lijun
 * @Date 2021/2/20 9:21
 */
@Service
public class TextAchievementsTempServiceImpl implements TextAchievementsTempService {

    @Autowired
    TextAchievementsTempRepository textAchievementsTempRepository;

    @Override
    public List<TextAchievementsTemp>  upload(MultipartFile multipartFile, HttpServletRequest request) throws IOException {
        List<TextAchievementsTemp> textAchievementsList = new ArrayList<>();
        if(multipartFile != null){
            String fjName = UUID.randomUUID() + "_" + multipartFile.getOriginalFilename();
            String pathname = request.getSession(true).getServletContext().getRealPath("//") + Constants.ACHIEVEMENTS_TEXT_FILE + fjName;
            if (!FileUtil.fileUpload(multipartFile, pathname)) {
                File file = new File(pathname);
                String filePath = "";
                if(file.exists()){
                    filePath = file.getAbsolutePath();
                }
                //Excel工具项
                InputStream inputStream = new FileInputStream(filePath);
                XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
                XSSFSheet sheet = workbook.getSheetAt(0);
                for(Row row : sheet){
                    if(row.getRowNum() == 0){
                        continue;
                    }
                    TextAchievementsTemp textAchievements = new TextAchievementsTemp();
                    //设置单元格类型
                    row.getCell(0).setCellType(CellType.STRING);
                    //序号
                    String serialNum = row.getCell(0).getStringCellValue();
                    //环境管控单元编码
                    String unitCode = row.getCell(1).getStringCellValue();
                    //单元名称
                    String unitName = row.getCell(2).getStringCellValue();
                    //省
                    String province = row.getCell(3).getStringCellValue();
                    //师
                    String division = row.getCell(4).getStringCellValue();
                    //团
                    String regiment = row.getCell(5).getStringCellValue();
                    //管控单元分类
                    String unitClassify = row.getCell(6).getStringCellValue();
                    //空间布局约束
                    String spaceRestrain = row.getCell(7).getStringCellValue();
                    //污染物排放管控
                    String pollutantRestrain = row.getCell(8).getStringCellValue();
                    //环境风险防控
                    String environmentRestrain = row.getCell(9).getStringCellValue();
                    //资源利用效率
                    String resourceUtilization = row.getCell(10).getStringCellValue();

                    textAchievements.setUnitCode(unitCode);
                    textAchievements.setUnitName(unitName);
                    textAchievements.setProvince(province);
                    textAchievements.setDivision(division);
                    textAchievements.setRegiment(regiment);
                    textAchievements.setUnitClassify(unitClassify);
                    textAchievements.setSpaceRestrain(spaceRestrain);
                    textAchievements.setPollutantRestrain(pollutantRestrain);
                    textAchievements.setEnvironmentRestrain(environmentRestrain);
                    textAchievements.setResourceUtilization(resourceUtilization);
                    textAchievementsList.add(textAchievements);
                }
            }
        }
        return textAchievementsList;
    }

    @Override
    public ResultState saveAll(List<TextAchievementsTemp> textAchievementsTemps) {
        List<TextAchievementsTemp> textAchievementsTemps1 = textAchievementsTempRepository.saveAll(textAchievementsTemps);
        return textAchievementsTemps1 != null && textAchievementsTemps1.size() > 0 ? new ResultState(StateInfo.SAVE_OK.getCode(), StateInfo.SAVE_OK.getMsg(), textAchievementsTemps1) :
                new ResultState(StateInfo.SAVE_ERROR.getCode(), StateInfo.SAVE_ERROR.getMsg());
    }
}
